﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using com.ClickCricket.bll;

/// <summary>
/// Summary description for MatchLinqProvider
/// </summary>
/// 
namespace com.ClickCricket.dal
{
    public class MatchLinqProvider : IMatchProvider
    {
        public MatchLinqProvider()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public List<Match> getMatches(int cID)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                List<Match> _matchList = new List<Match>();

                var match = from aMatch in dc.Matches
                            where aMatch.SeriesID == cID
                            select aMatch;
                foreach (var m in match)
                {
                    _matchList.Add(extractMatch(m));
                }
                return _matchList;
            }
        }

        public List<Match> GetMatches(int seriesID)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                List<Match> _matchList = new List<Match>();

                var match = from aMatch in dc.Matches
                            where aMatch.SeriesID == seriesID
                            select aMatch;
                foreach (var m in match)
                {
                    _matchList.Add(extractMatch(m));
                }
                return _matchList;
            }
        }

        public bool getOnGoingSeries(int SeriesID)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                var matches = from aMatch in dc.Matches
                              where aMatch.SeriesID == SeriesID
                              orderby aMatch.Date ascending
                              select aMatch;
                List<Match> _matchList = new List<Match>();
                foreach (var match in matches)
                {
                    _matchList.Add(extractMatch(match));
                }
                if (System.DateTime.Now >= _matchList[0].Date && System.DateTime.Now <= _matchList[_matchList.Count - 1].Date)
                    return true;
                else
                    return false;
            }
        }

        public bool getUpComingSeries(int SeriesID)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                var matches = from aMatch in dc.Matches
                              where aMatch.SeriesID == SeriesID
                              orderby aMatch.Date ascending
                              select aMatch;
                List<Match> _matchList = new List<Match>();
                foreach (var match in matches)
                {
                    _matchList.Add(extractMatch(match));
                }
                if (System.DateTime.Now < _matchList[0].Date)
                    return true;
                else
                    return false;
            }
        }

        public bool getCompletedSeries(int SeriesID)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                var matches = from aMatch in dc.Matches
                              where aMatch.SeriesID == SeriesID
                              orderby aMatch.Date ascending
                              select aMatch;
                List<Match> _matchList = new List<Match>();
                foreach (var match in matches)
                {
                    _matchList.Add(extractMatch(match));
                }
                if (System.DateTime.Now > _matchList[0].Date && System.DateTime.Now > _matchList[_matchList.Count - 1].Date)
                    return true;
                else
                    return false;
            }
        }              
        private Match extractMatch(Entity.Match m)
        {
            Match aMatch = new Match(
                m.MatchID,
                Convert.ToInt16(m.GameFormatID),
                m.Venue,
                Convert.ToDateTime(m.Date),
                Convert.ToInt16(m.SeriesID),
                Convert.ToInt16(m.WinnerID));
            return aMatch;
        }
    }
}
